package com.huatai.common.convert;

/**
 * @program: ht-system
 * @description: convert
 * @author: songxiude
 * @create: 2023-01-31 14:32
 **/

import com.fasterxml.jackson.annotation.JsonValue;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import com.huatai.common.util.ConvertUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.lang.Nullable;

public class EnumToStringConverter implements ConditionalGenericConverter {
    private static final Logger log = LoggerFactory.getLogger(EnumToStringConverter.class);
    private static final ConcurrentMap<Class<?>, AccessibleObject> ENUM_CACHE_MAP = new ConcurrentHashMap(8);

    public EnumToStringConverter() {
    }

    @Nullable
    private static AccessibleObject getAnnotation(Class<?> clazz) {
        Set<AccessibleObject> accessibleObjects = new HashSet();
        Field[] fields = clazz.getDeclaredFields();
        Collections.addAll(accessibleObjects, fields);
        Method[] methods = clazz.getDeclaredMethods();
        Collections.addAll(accessibleObjects, methods);
        Iterator var4 = accessibleObjects.iterator();

        AccessibleObject accessibleObject;
        JsonValue jsonValue;
        do {
            if (!var4.hasNext()) {
                return null;
            }

            accessibleObject = (AccessibleObject)var4.next();
            jsonValue = (JsonValue)accessibleObject.getAnnotation(JsonValue.class);
        } while(jsonValue == null || !jsonValue.value());

        accessibleObject.setAccessible(true);
        return accessibleObject;
    }

    public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
        return true;
    }

    public Set<GenericConverter.ConvertiblePair> getConvertibleTypes() {
        Set<GenericConverter.ConvertiblePair> pairSet = new HashSet(3);
        pairSet.add(new GenericConverter.ConvertiblePair(Enum.class, String.class));
        pairSet.add(new GenericConverter.ConvertiblePair(Enum.class, Integer.class));
        pairSet.add(new GenericConverter.ConvertiblePair(Enum.class, Long.class));
        return Collections.unmodifiableSet(pairSet);
    }

    public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
        if (source == null) {
            return null;
        } else {
            Class<?> sourceClazz = sourceType.getType();
            AccessibleObject accessibleObject = (AccessibleObject)ENUM_CACHE_MAP.computeIfAbsent(sourceClazz, EnumToStringConverter::getAnnotation);
            Class<?> targetClazz = targetType.getType();
            if (accessibleObject == null) {
                if (String.class == targetClazz) {
                    return ((Enum)source).name();
                } else {
                    int ordinal = ((Enum)source).ordinal();
                    return ConvertUtil.convert(ordinal, targetClazz);
                }
            } else {
                try {
                    return invoke(sourceClazz, accessibleObject, source, targetClazz);
                } catch (Exception var8) {
                    log.error(var8.getMessage(), var8);
                    return null;
                }
            }
        }
    }

    @Nullable
    private static Object invoke(Class<?> clazz, AccessibleObject accessibleObject, Object source, Class<?> targetClazz) throws IllegalAccessException, InvocationTargetException {
        Object value = null;
        if (accessibleObject instanceof Field) {
            Field field = (Field)accessibleObject;
            value = field.get(source);
        } else if (accessibleObject instanceof Method) {
            Method method = (Method)accessibleObject;
            Class<?> paramType = method.getParameterTypes()[0];
            Object object = ConvertUtil.convert(source, paramType);
            value = method.invoke(clazz, object);
        }

        return value == null ? null : ConvertUtil.convert(value, targetClazz);
    }
}

